package com.jsjvm.server.dbgproxy;

import java.io.IOException;
import java.util.HashMap;

import com.jsjvm.server.dbgproxy.JDWPCommandSet.JDWPCommandHandler;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPInputStream;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPOutputStream;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPRequestHeader;
import com.jsjvm.shared.vmcmds.VMCommand;

/**
 * 
 * Every command set implements command handlers for each of its commands as
 * static inner classes. This is the interface for a command handler.
 * 
 * @author ctalau
 *
 */
@SuppressWarnings("serial")
public abstract class JDWPCommandSet extends HashMap<Integer, JDWPCommandHandler> {
	
	public static abstract class JDWPCommandHandler {
		public static final short NOT_IMPLEMENTED = 99;
		public static final short ABSENT_INFORMATION = 101;
		
		/**
		 * Handles a command from the front-end. If it cannot handle it internally
		 * it composes a VMCommand for the VM.
		 */
		public abstract VMCommand handleCommand(JDWPRequestHeader hdr,
				JDWPInputStream is, JDWPOutputStream os) throws IOException;
	}	
	
	public abstract int getCommandSetNumber();
	
	private static JDWPCommandHandler h = new JDWPCommandHandler (){
		@Override
		public VMCommand handleCommand(JDWPRequestHeader hdr, JDWPInputStream is,
				JDWPOutputStream os) throws IOException {
			os.replyWithError(hdr, NOT_IMPLEMENTED);
			return null;
		}
	};
	
	public static JDWPCommandHandler getNIHandler(){
		return h;
	}
}
